//
//  NSDate+Extension.swift
//  WeiBo18
//
//  Created by apple on 15/10/17.
//  Copyright © 2015年 apple. All rights reserved.
//

import Foundation


extension NSDate{
    
    
    /**
    判断是否是今年
    
    - parameter target: <#target description#>
    */
    func isThisYear(target: NSDate) -> Bool {
        //获取传入target的年的字符中,与当前时间的年的字符串,进行对比
        
        //获取到当前的时间
//        let currentDate = NSDate()
        
        /// 初始化一个时间格式化器
        let formatter = NSDateFormatter()
        //指定格式化的字符串格式
        formatter.dateFormat = "yyyy"
        
        //取到我们要对比的date的年份字符串
        let targetStr = formatter.stringFromDate(target)
        //取到当前时间的年份字符串
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
        
    }
    
    /**
    判断是否是今天
    
    - parameter target:
    */
    func isToday(target: NSDate) -> Bool {
        //取到当前时候的年月日,与target的年月日进行对比,如果字符串一样,就返回true
        
//        let currentDate = NSDate()
        
        
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        //格式化成字符串
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
    }
    
    /**
    判断是否是昨天
    
    - parameter target: <#target description#>
    
    - returns: <#return value description#>
    */
    func isYesterday(var target: NSDate) -> Bool {
        
        /// 取出当前时间
        var currentDate = NSDate()
        
        
        //target --> 2015-10-16 10:10:10(NSDate)
        //current --> 2015-10-17 11:10:10(NSDate)
        
        
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        //把date转成string 格式是 yyyy-MM-dd
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(currentDate)
        
        //如果现在想对比是否是相差一天的话,用字符串比较麻烦
        //所以说,需要转成nsdate去做逻辑
        
        //把string转成date 格式是 yyyy-MM-dd
        
        target = formatter.dateFromString(targetStr)!
        currentDate = formatter.dateFromString(currentStr)!
        
        //target --> 2015-10-16(NSDate)
        //current --> 2015-10-17(NSDate)
        
        //求出两个时间之间的间隔(单位:秒)
        let result = currentDate.timeIntervalSinceDate(target)
        
        if result == 24 * 60 * 60 {
            return true
        }
        printLog(result)
        
        return false
    }
    
    func isYesterday2(var target: NSDate) -> Bool {
        
        /// 取出当前时间
        var currentDate = NSDate()
        
        
        //target --> 2015-10-16 10:10:10(NSDate)
        //current --> 2015-10-17 11:10:10(NSDate)
        
        
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        //把date转成string 格式是 yyyy-MM-dd
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(currentDate)
        
        //如果现在想对比是否是相差一天的话,用字符串比较麻烦
        //所以说,需要转成nsdate去做逻辑
        
        //把string转成date 格式是 yyyy-MM-dd
        
        target = formatter.dateFromString(targetStr)!
        currentDate = formatter.dateFromString(currentStr)!
        
        
        //        NSCalendar
        
        //target --> 2015-10-16 10:10:10(NSDate)
        //current --> 2015-10-17 10:10:10(NSDate)
        //只获取相差多少秒 --> 24 * 60 * 60
        //相差多少分多少秒 --> 1440 分 0秒
        //相差多少小时多少分多少秒 --> 24小时0分0秒
        
        
        //target --> 2015-10-16(NSDate)
        //current --> 2015-10-17(NSDate)
        
        /// 获取到当前日历对象
        let calendar = NSCalendar.currentCalendar()
        // components(unitFlags: NSCalendarUnit, fromDate startingDate: NSDate, toDate resultDate: NSDate, options opts: NSCalendarOptions) -> NSDateComponents
        let components = calendar.components([NSCalendarUnit.NSDayCalendarUnit] , fromDate: target, toDate: currentDate, options: NSCalendarOptions.WrapComponents)
        
        //        printLog("year:\(components.year),month:\(components.month),day:\(components.day)")
        if (components.day == 1){
            return true
        }
        
        //        //求出两个时间之间的间隔(单位:秒)
        //        let result = currentDate.timeIntervalSinceDate(target)
        //
        //        if result == 24 * 60 * 60 {
        //            return true
        //        }
        //        printLog(result)
        
        return false
    }
}